package com.tour.modules.web.controller.api;

import com.tour.modules.web.entity.TUserEntity;
import com.tour.modules.web.service.TUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
public class TUserApi {

    @Autowired
    private TUserService tUserService;
    /**
     * 登录
     */
    @RequestMapping("/api/tuser/login")
    @ResponseBody
    public String login(@RequestParam("userName") String userName,@RequestParam("userPass") String userPass,HttpServletRequest request){
        TUserEntity t1=tUserService.login(userName);
        if(t1!=null){
            if(t1.getUserPass().equals(userPass)){
                request.getSession().setAttribute("user_login",t1);
                return "200";
            }
            return "500";
        }
        return "500";
    }
    @RequestMapping("/api/tuser/regedit")
    public String regedit(HttpServletRequest request) {
        TUserEntity t=new TUserEntity();
        t.setUserName(request.getParameter("userName"));
        t.setUserPass(request.getParameter("userPass"));
        t.setEmail(request.getParameter("email"));
        t.setMobile(request.getParameter("mobile"));
        tUserService.save(t);
        return "redirect:/front/user_login.jsp";
    }
    @RequestMapping("/api/tuser/getuser")
    @ResponseBody
    public Map getUser(@RequestParam("userName") String userName){
        Map map=new HashMap();
        TUserEntity t1=tUserService.login(userName);
        if(t1==null){
            map.put("code","500");
            return map;
        }
        map.put("code","200");
        return map;
    }
    @RequestMapping("/api/user/logout")
    public String logout(HttpServletRequest request){
       request.getSession().removeAttribute("user_login");
        return "redirect:/front/user_login.jsp";
    }
}
